package com.glls.java2217.controller;

import com.glls.java2217.common.LayuiPageVo;
import com.glls.java2217.common.R;
import com.glls.java2217.pojo.Student;
import com.glls.java2217.service.StudentService;
import com.glls.java2217.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @date 2023-01-05
 * @desc
 */
@RequestMapping("/student")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/studentList")
    public LayuiPageVo<Student> studentPageData(@RequestParam Map<String, Object> params){
        System.out.println(params);

        LayuiPageVo<Student> result = studentService.studentPageData(params);

        return result;
    }



    @RequestMapping("/deleteStudent")
    public R deleteStudentById(Integer id){
        int result = studentService.deleteStudentById(id);

        if(result==1){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @RequestMapping("/address")
    public R getAddress(){
        List<String> address = studentService.getAddress();
        return R.ok().data("address",address);
    }

    @RequestMapping("/upload")
    public R uploadPic(MultipartFile file){

        //得到文件的名字
        String originalFilename = file.getOriginalFilename();
        //将文件 转为输入流
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            String url = OssUtil.uploadFile(inputStream, originalFilename);
            return R.ok().data("url",url);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return R.error();

    }

}
